﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MultiThread
{
    public class TreadPoolOperator
    {
        private static int count = 50;
        private static List<string> eggs;
        private static object obj = new object();
        public static void CreateMultiThread()
        {
            eggs = new List<string>();
            for (int i = 1; i <= 50; i++)
            {
                string egg = "egg" + i.ToString();
                eggs.Add(egg);
            }


            ThreadPool.QueueUserWorkItem(TreadPoolOperator.RunThread1);
            ThreadPool.QueueUserWorkItem(TreadPoolOperator.RunThread1);
            ThreadPool.QueueUserWorkItem(TreadPoolOperator.RunThread1);
            ThreadPool.QueueUserWorkItem(TreadPoolOperator.RunThread1);

        }
        public static void RunThread1(object obj)
        {
            
            Console.WriteLine(obj??"null");
            GetAnEgg();
            Thread.Sleep(1000);
            RunThread1( obj);

        }
        public static void RunThread2(object obj)
        {
            Console.WriteLine(obj ?? "null");
            GetAnEgg();
            Thread.Sleep(2000);
            RunThread2(obj);
        }
        public static void RunThread3(object obj)
        {
            Console.WriteLine(obj ?? "null");
            GetAnEgg();
            Thread.Sleep(3000);
            RunThread3(obj);
        }
        public static void RunThread4(object obj)
        {
            Console.WriteLine(obj ?? "null");
            GetAnEgg();
            Thread.Sleep(4000);
            RunThread4(obj);
        }
        public static void GetAnEgg()
        {
            lock (obj)
            {
                if (eggs.Count == 0)
                {

                    return;
                }
                string egg = eggs[0];
                Console.WriteLine(Thread.CurrentThread.Name + "=====" + egg + "==============" + DateTime.Now.ToString("mm--ss"));
                eggs.Remove(egg);


            }
        }
    }
}
